home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fatted Calf
/
The Fatted Calf.iso
/
Applications
/
Unix
/
MOTD
/
Source
/
trimwtmp.c
< prev
Wrap
C/C++ Source or Header
|
1992-11-09
|
2KB
|
76 lines
/* Program: trimwtmp.c - Utility to prune Unix wtmp log files.
*
* Usage: trimwtmp [days] [wtmpfile]
*
* Defaults: 30 days "/usr/adm/wtmp"
*
* Compile: cc -o trimwtmp trimwtmp.c
*
* "mike" (Mike Macgirvin) Mike_Macgirvin@MED.Stanford.EDU
* Unix Server Administrator CAMIS Computer Project
*
* Modified for NeXT & ANSI-C by:
*
* Christopher Lane (Christopher_Lane@Med.Stanford.EDU)
* Symbolic Systems Resources Group, Knowledge Systems Laboratory
*
* Stanford University
*/
#include <c.h>
#include <libc.h>
#include <utmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define TRIM_TIME 30
#define PATH_WTMP "/usr/adm/wtmp"
#define SECONDS_DAY (60 * 60 * 24)
typedef enum {PROGRAM = 0, DAYS, FILENAME, ARGC} ARGUMENTS;
void main(int argc, char *argv[])
{
int days;
time_t when;
struct utmp ut;
FILE *fin, *fout;
char filename[MAXPATHLEN], temp_filename[MAXPATHLEN];
if(argc > ARGC) {
(void) fprintf(stderr, "usage: %s [days] [wtmpfile]\n", argv[PROGRAM]);
exit(EXIT_FAILURE);
}
(void) strcpy(filename, (argc > FILENAME) ? argv[FILENAME] : PATH_WTMP);
if(argc <= DAYS || (days = atoi(argv[1])) <= 0) days = TRIM_TIME;
when = time(NULL) - (days * SECONDS_DAY);
(void) sprintf(temp_filename, "%s%d", filename, getpid());
#ifdef DEBUG
(void) fprintf(stderr, "using \"%s\", \"%s\", trimming %d days\n", filename, temp_filename, days);
#endif
if((fout = fopen(temp_filename, "w")) == NULL) {
(void) fprintf(stderr, "%s: Can't open temporary file for write: %s\n", argv[PROGRAM], temp_filename);
exit(EXIT_FAILURE);
}
if((fin = fopen(filename, "r")) == NULL) {
(void) fprintf(stderr, "%s: Can't open wtmp file for read: %s\n", argv[PROGRAM], filename);
exit(EXIT_FAILURE);
}
while(fread(&ut, sizeof(struct utmp), 1, fin) != 0)
if((time_t) ut.ut_time > when) (void) fwrite(&ut, sizeof(struct utmp), 1, fout);
(void) fclose(fin);
(void) fclose(fout);
#ifndef DEBUG
if(unlink(filename) != CERROR) (void) rename(temp_filename, filename);
#endif
exit(EXIT_SUCCESS);
}